﻿//
// Copyright 2008-2011 by Burt Harris
// Licensed under the Microsoft Public License (Ms-PL)
// See: http://www.codeplex.com/yaml/license
//

using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace YaTools.Yaml
{
    public sealed partial class Dump
    {
        public abstract partial class Presenter
        {
            public abstract partial class Yaml
            {
                public class Flow : Yaml
                {
                    private readonly List<Tag> _suppressTags;

                    public Flow()
                    {
                        this._suppressTags = new List<Tag>
                                                 {
                                                     Tag.String,
                                                     Tag.Integer,
                                                     Tag.Float,
                                                     Tag.Sequence,
                                                     Tag.Map
                                                 };
                    }

                    private static Flow _instance;

                    public static Flow Instance
                    {
                        get { return _instance ?? (_instance = new Flow()); }
                    }

                    // ReSharper disable UnusedMember.Local
                    [ContractInvariantMethod]
                    private void ObjectInvariant()
                    {
                        Contract.Invariant(_suppressTags != null);
                    }

                    // ReSharper restore UnusedMember.Local

                    //private void ShouldDumpObject(Dump Dump, object value)
                    //{
                    //    Contract.Assert(Dump != null);
                    //    Contract.Assume(value != null);

                    //    switch (Dump.Kind)
                    //    {
                    //        case Kind.Document:
                    //            Dump.WriteLine("%YAML 1.2");
                    //            Dump.WriteLine("---");
                    //            break;

                    //        case Kind.Mapping:
                    //        case Kind.Sequence:
                    //            bool first = Dump.Count == 0;

                    //            if (first)
                    //            {
                    //                Dump.WriteLine();
                    //            }
                    //            else
                    //            {
                    //                if (!Dump.PreviousWasKey)
                    //                    Dump.WriteLine(',');
                    //            }

                    //            if (!Dump.PreviousWasKey)
                    //                Dump.Writer.WriteSpaces(IndentSize(Dump));

                    //            var keyString = value as string;
                    //            bool plainKey = keyString != null && keyString.Length < 80 ||
                    //                            Scalar.IsPlainKey(keyString, true);

                    //            if (Dump.IsKey && !plainKey)
                    //                Dump.Writer.Write("? ");
                    //            else if (Dump.PreviousWasKey)
                    //                Dump.Writer.Write(": ");

                    //            if (_suppressTag.Contains(Dump.Tag) ||
                    //                value.GetType() == Dump.ExpectedType)
                    //            {
                    //                Dump.Tag = null;
                    //            }
                    //            break;

                    //        default:
                    //            throw new InvalidOperationException("Unexpected Kind");
                    //    }
                    //}

                    public override void PresentNull(Dump dump)
                    {
                        dump.Write('~');
                    }

                    public override void PresentScalar(Dump dump, string value, Tag tag)
                    {
                        dump.WriteYamlScalar(value, Scalar.Style.Default, Scalar.Chomping.Chomp, true);
                    }
                }
            }
        }
    }
}